package com.controller;

import com.entities.CommonResult;
import com.entities.Payment;
import com.lb.LoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 04-23-19:32
 */

@RestController
public class OrderController {

//    public static final String payment_URL="http://localhost:8080";
//    此处采用负载均衡的方式进行，以名字的方式去eureka去查询地址
    public static final String payment_URL="http://payment-client";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private LoadBalancer loadBalancer ;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id ){
        CommonResult commonResult1 = restTemplate.getForObject(payment_URL + "/payment/get/" + id, CommonResult.class,id);
//        CommonResult commonResult = restTemplate.postForObject(payment_URL + "/payment/get/" + id, id, CommonResult.class);
        return  commonResult1 ;
    }

    @GetMapping("/consumer/payment/insert")
    public CommonResult<Payment> createPayment(Payment payment){
        CommonResult commonResult = restTemplate.postForObject(payment_URL + "/payment/insert", payment, CommonResult.class);
        return commonResult;
    }

    //此处来测试 restTemplate 中的Api
    @GetMapping("/consumer/payment/getEntity/{id}")
    public CommonResult<Payment> testForEntity(@PathVariable("id")Long id){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(payment_URL + "/payment/get/" + id, CommonResult.class, id);
        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }else {
            return new CommonResult<>("操作失败",444,null);
        }
    }

    @GetMapping("/consumer/payment/postEntity/")
    public CommonResult<Payment> testForPostEntity(Payment payment){
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(payment_URL + "/payment/insert", payment, CommonResult.class);
        return entity.getBody();
    }

    @GetMapping("/consumer/payment/lb")
    public String testForRobin(){
        //获取所有注册进eureka的微服务；
        List<ServiceInstance> instances = discoveryClient.getInstances("PAYMENT-CLIENT");
        //调用自定义接口APi
        ServiceInstance instanceForRobin = loadBalancer.instance(instances);
        URI uri = instanceForRobin.getUri();

        //消费者微服务调用 生产者微服务 (json序列话和反序劣化)
        String paymentPort = restTemplate.getForObject(uri + "/payment/lb", String.class);
        return paymentPort ;

    }

}
